package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.Qiniu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        model.addAttribute("result",strategyService.query(qo));
        model.addAttribute("places",regionService.query());
        return "strategy/list";
    }

    @RequestMapping("/saveOrUpdate")@ResponseBody
    public Object saveOrUpdate(Strategy strategy, MultipartFile image){

        JSONResult json = new JSONResult();
        try {
            if (image != null) {
//                String upload = UploadUtil.upload(image, UploadUtil.IMG_BASE_PATH + "/upload");
                String upload = Qiniu.upLoadImage(image.getBytes());
                strategy.setCoverUrl(upload);
            }
            strategyService.saveOrUpdate(strategy);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
}
